const express = require("express");
const router = express.Router();
const Joi = require("joi");
const { upload, merge, verify } = require("../services/uploadService");
const { getDataListByCate, getFile } = require("../services/searchService");
const { update, del } = require("../services/fileTableOperationService");

/**  
 * @api {post} /upload 文件上传  
 * @apiGroup upload  
 * @apiVersion 1.0.0  
 * @apiName upload  
 * @apiDescription 文件上传 
 *   
 * @apiParam {String} fileHash 文件hash值  
 * @apiParam {String} fileSize 文件大小   
 * @apiParam {String} fileName 文件名   
 * @apiParam {String} index 0  当前分片的下标 
 * @apiParam {String} chunkFile 二进制文件   
 * @apiParam {String} chunkHash 分片片段的文件hash值   
 * @apiParam {String} chunkSize 分片大小   
 * @apiParam {String} chunkNumber 分片数量   
 *  
 * @apiSuccess {String} id The unique ID of the created user.  
 * @apiSuccess {String} username The username of the created user.  
 *  
 * @apiSuccessExample Success-Response:  
     HTTP/1.1 0 OK  
  {
    "code": 0,
    "msg": "单片上传完成",
    "data": {
        "fileHash": [
            "e202cd86c3789445723d385d109bdfe024.2次论文撰写名单含专业年级，老师"
        ],
        "chunkHash": [
            "e202cd86c3789445723d385d109bdfe0-0"
        ],
        "fileName": [
            "24.2次论文撰写名单含专业年级，老师.xlsx"
        ]
    }
  }
 *  
 * @apiError {String} error Detailed error message.  
 *  
 * @apiErrorExample Error-Response:  
     HTTP/1.1 -1 Bad Request  
     {  
       "error": ""  
     }  
 */
router.post("/upload", upload);
/**  
 * @api {post} /merge 合并分片  
 * @apiGroup upload  
 * @apiVersion 1.0.0  
 * @apiName merge  
 * @apiDescription 合并分片
 *   
 * @apiParam {Numer} chunkSize 分片大小  
 * @apiParam {String} fileHash 文件hash值   
 * @apiParam {String} fileName 文件名   
 * @apiParam {Numer} fileSize 文件实际大小   
 *  
 * @apiSuccess {String} id The unique ID of the created user.  
 * @apiSuccess {String} username The username of the created user.  
 *  
 * @apiSuccessExample Success-Response:  
     HTTP/1.1 0 OK  
     {  
       "username": "admin",  
       "password": "admin123"  
     }  
 *  
 * @apiError {String} error Detailed error message.  
 *  
 * @apiErrorExample Error-Response:  
     HTTP/1.1 -1 Bad Request  
     {  
       "error": "Invalid username"  
     }  
 */
router.post("/merge", function (req, res, next) {
  const schema = Joi.object({
    chunkSize: Joi.required(),
    fileHash: Joi.required(),
    fileName: Joi.required(),
    fileSize: Joi.required(),
    uid: Joi.required(),
    contentType: Joi.required(),
  });
  // 使用schema来验证表单数据
  const { error, value } = schema.validate({ ...req.body });
  if (error) {
    return res.status(400).json({
      code: -1,
      data: null,
      msg: error.message,
    });
  }
  merge(req, res, next);
});
/**  
 * @api {post} /verify 注册  
 * @apiGroup upload  
 * @apiVersion 1.0.0  
 * @apiName verify  
 * @apiDescription 验证文件是否已经上传了，已经上传则直接写入库中
 *   
 * @apiParam {String} username 用户名称  
 * @apiParam {String} password 用户密码   
 *  
 * @apiSuccess {String} id The unique ID of the created user.  
 * @apiSuccess {String} username The username of the created user.  
 *  
 * @apiSuccessExample Success-Response:  
     HTTP/1.1 0 OK  
     {  
       "username": "admin",  
       "password": "admin123"  
     }  
 *  
 * @apiError {String} error Detailed error message.  
 *  
 * @apiErrorExample Error-Response:  
     HTTP/1.1 -1 Bad Request  
     {  
       "error": "Invalid username"  
     }  
 */
router.post("/verify", verify);

/**  
 * @api {post} /getDataListByCate 通过category获取文件列表  
 * @apiGroup search  
 * @apiVersion 1.0.0  
 * @apiName getDataListByCate  
 * @apiDescription 验证文件是否已经上传了，已经上传则直接写入库中
 *   
 * @apiParam {String} username 用户名称  
 * @apiParam {String} password 用户密码   
 *  
 * @apiSuccess {String} id The unique ID of the created user.  
 * @apiSuccess {String} username The username of the created user.  
 *  
 * @apiSuccessExample Success-Response:  
     HTTP/1.1 0 OK  
     {  
       "username": "admin",  
       "password": "admin123"  
     }  
 *  
 * @apiError {String} error Detailed error message.  
 *  
 * @apiErrorExample Error-Response:  
     HTTP/1.1 -1 Bad Request  
     {  
       "error": "Invalid username"  
     }  
 */
router.get("/getDataListByCate", function (req, res, next) {
  getDataListByCate(req, res, next);
});
router.get("/getFile", function (req, res, next) {
  // const schema = Joi.object({
  //   filePath: Joi.required(),
  // });
  // // 使用schema来验证表单数据
  // const { error, value } = schema.validate({ ...req.query });
  // if (error) {
  //   return res.status(400).json({
  //     code: -1,
  //     data: null,
  //     msg: error.message,
  //   });
  // }
  getFile(req, res, next);
});

router.post("/update", function (req, res, next) {
  const schema = Joi.object({
    fileName: Joi.string().required(),
    id: Joi.string().required(),
  });
  // 使用schema来验证表单数据
  const { error, value } = schema.validate({ ...req.body });
  if (error) {
    return res.status(400).json({
      code: -1,
      data: null,
      msg: error.message,
    });
  }
  update(req, res, next);
});
router.post("/del", function (req, res, next) {
  const schema = Joi.object({
    id: Joi.string().required(),
  });
  // 使用schema来验证表单数据
  const { error, value } = schema.validate({ ...req.body });
  if (error) {
    return res.status(400).json({
      code: -1,
      data: null,
      msg: error.message,
    });
  }
  del(req, res, next);
});

module.exports = router;
